package com.k2.exstrasensorytypredictor.containers;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

import com.k2.exstrasensorytypredictor.interfaces.IConstants;

public class GfxResourceContainer implements IConstants {
	private static GfxResourceContainer inst = null;
	private Context ctx;
	
	//---- resources declaration
	private BitmapDrawable mainBg;
	private BitmapDrawable mainBgMs;
	private Bitmap scr2Text;
	private Bitmap mainText;
	private Bitmap cardScrText;
	private Bitmap cardShirt;
	private Bitmap pScrTitle;
	private Bitmap[] colors;
	private Bitmap[] pScrBitmaps;
	private Bitmap[][] mScreenButtonBg;
	private Bitmap[][] numScreenNumbers;
	private Bitmap[][] geometry;
	private Bitmap[][] cards;
	//--------------------------
	
	private GfxResourceContainer(Context ctx){
		this.ctx = ctx;
		this.loadResources();
	}
	
	public static GfxResourceContainer getInstance(Context context) {
		if (inst == null){
			inst = new GfxResourceContainer(context);
		}
		
		return inst;
	}
	
	public static GfxResourceContainer getInstance() {
		if (inst == null){
			return inst;
		}
		
		return null;
	}
	
	public void loadResources(){
		BitmapFactory.Options bfOptions = new BitmapFactory.Options();
		bfOptions.inPurgeable = true;
		bfOptions.inInputShareable = true;
		
		try {
			this.mainBg = (BitmapDrawable) BitmapDrawable.createFromStream(this.ctx.getAssets().open(BACKGROUND_IMG), "");
			this.mainBgMs = (BitmapDrawable) BitmapDrawable.createFromStream(this.ctx.getAssets().open(BACKGROUND_MS_IMG), "");
			this.mainText = BitmapFactory.decodeStream(this.ctx.getAssets().open(M_SCR_TEXT), null, bfOptions);
			this.scr2Text = BitmapFactory.decodeStream(this.ctx.getAssets().open(SCREEN2_TEXT), null, bfOptions);
			Log.d(LOADING_RESOURCES, "main bg loaded...");
		} catch (IOException e) {
			Log.e(LOADING_RESOURCES, "maing bg loading failed!!!");
		} catch (OutOfMemoryError e) {
			Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
		}
		
		mScreenButtonBg = new Bitmap[M_SCR_BUTTONS.length][];
		for (int i = 0; i < mScreenButtonBg.length; i++) {
			mScreenButtonBg[i] = new Bitmap[M_SCR_BUTTONS[i].length];
			for (int j = 0; j < mScreenButtonBg[i].length; j++) {
				try {
					mScreenButtonBg[i][j] = BitmapFactory.decodeStream(this.ctx.getAssets().open(M_SCR_BUTTONS[i][j]), null, bfOptions);
					Log.d(LOADING_RESOURCES, M_SCR_BUTTONS[i][j]+" button bg loaded...");
				} catch (IOException e) {
					Log.e(LOADING_RESOURCES, M_SCR_BUTTONS[i][j] + " loading failed!!!");
				} catch (OutOfMemoryError e) {
					Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
				}
			}
		}
		
		numScreenNumbers = new Bitmap[NUMBERS.length][];
		for (int i = 0; i < numScreenNumbers.length; i++) {
			numScreenNumbers[i] = new Bitmap[NUMBERS[i].length];
			for (int j = 0; j < numScreenNumbers[i].length; j++) {
				try {
					numScreenNumbers[i][j] = BitmapFactory.decodeStream(this.ctx.getAssets().open(NUMBERS[i][j]), null, bfOptions);
					Log.d(LOADING_RESOURCES, NUMBERS[i][j]+" button bg loaded...");
				} catch (IOException e) {
					Log.e(LOADING_RESOURCES, NUMBERS[i][j] + " loading failed!!!");
				} catch (OutOfMemoryError e) {
					Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
				}
			}
		}
		
		colors = new Bitmap[COLOR_PICTURES.length];
		for (int i = 0; i < colors.length; i++) {
			try {
				colors[i] = BitmapFactory.decodeStream(this.ctx.getAssets().open(COLOR_PICTURES[i]), null, bfOptions);
				Log.d(LOADING_RESOURCES, COLOR_PICTURES[i] + " loaded...");
			} catch (IOException e) {
				Log.e(LOADING_RESOURCES, COLOR_PICTURES[i] + " loading failed!!!");
			} catch (OutOfMemoryError e) {
				Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
			}
		}
		
		geometry = new Bitmap[GEOMETRY_PICTURES.length][];
		for (int i = 0; i < geometry.length; i++) {
			geometry[i] = new Bitmap[GEOMETRY_PICTURES[i].length];
			for (int j = 0; j < geometry[i].length; j++) {
				try {
					geometry[i][j] = BitmapFactory.decodeStream(this.ctx.getAssets().open(GEOMETRY_PICTURES[i][j]), null, bfOptions);
					Log.d(LOADING_RESOURCES, GEOMETRY_PICTURES[i][j] + " loaded...");
				} catch (IOException e) {
					Log.e(LOADING_RESOURCES, GEOMETRY_PICTURES[i][j] + " loading failed!!!");
				} catch (OutOfMemoryError e) {
					Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
				}
			}
		}
		try {
			cardScrText = BitmapFactory.decodeStream(this.ctx.getAssets().open(CARD_SCR_TEXT), null, bfOptions);
			Log.d(LOADING_RESOURCES, CARD_SCR_TEXT + " loaded...");
		} catch (IOException e) {
			Log.e(LOADING_RESOURCES, CARD_SCR_TEXT + " loading failed!!!");
		} catch (OutOfMemoryError e) {
			Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
		}
		try {
			cardShirt = BitmapFactory.decodeStream(this.ctx.getAssets().open(CARD_SCR_SHIRT), null, bfOptions);
			Log.d(LOADING_RESOURCES, CARD_SCR_SHIRT + " loaded...");
		} catch (IOException e) {
			Log.e(LOADING_RESOURCES, CARD_SCR_SHIRT + " loading failed!!!");
		} catch (OutOfMemoryError e) {
			Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
		}
		cards = new Bitmap[CARDS.length][];
		for (int i = 0; i < cards.length; i++) {
			cards[i] = new Bitmap[CARDS[i].length];
			for (int j = 0; j < cards[i].length; j++) {
				try {
					cards[i][j] = BitmapFactory.decodeStream(this.ctx.getAssets().open(CARDS[i][j]), null, bfOptions);
					Log.d(LOADING_RESOURCES, CARDS[i][j]+" button bg loaded...");
				} catch (IOException e) {
					Log.e(LOADING_RESOURCES, CARDS[i][j] + " loading failed!!!");
				} catch (OutOfMemoryError e) {
					Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
				}
			}
		}
		try {
			pScrTitle = BitmapFactory.decodeStream(this.ctx.getAssets().open(PICT_SCR_TITLE), null, bfOptions);
			Log.d(LOADING_RESOURCES, PICT_SCR_TITLE+" loaded...");
		} catch (IOException e) {
			Log.e(LOADING_RESOURCES, PICT_SCR_TITLE + " loading failed!!!");
		} catch (OutOfMemoryError e) {
			Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
		}
		pScrBitmaps = new Bitmap[PICT_SCREEN_IMAGES.length];
		for (int i = 0; i < pScrBitmaps.length; i++) {
			try {
				pScrBitmaps[i] = BitmapFactory.decodeStream(this.ctx.getAssets().open(PICT_SCREEN_IMAGES[i]), null, bfOptions);
				Log.d(LOADING_RESOURCES, PICT_SCREEN_IMAGES[i] + " loaded...");
			} catch (IOException e) {
				Log.e(LOADING_RESOURCES, PICT_SCREEN_IMAGES[i] + " loading failed!!!");
			} catch (OutOfMemoryError e) {
				Log.e(LOADING_RESOURCES, "OUT OF MEMORY!!!");
			}
		}
	}
	
//	private Bitmap decodeBitmap(InputStream is){
//		Bitmap b = null;
//	    try {
//	        //Decode image size
//	        BitmapFactory.Options o = new BitmapFactory.Options();
//	        o.inJustDecodeBounds = true;
//
////	        FileInputStream fis = new FileInputStream(f);
//	        BitmapFactory.decodeStream(is, null, o);
////	        fis.close();
//
//	        int scale = 1;
//	        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
//	            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / 
//	               (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
//	        }
//
//	        //Decode with inSampleSize
//	        BitmapFactory.Options o2 = new BitmapFactory.Options();
//	        o2.inSampleSize = scale;
////	        fis = new FileInputStream(f);
//	        b = BitmapFactory.decodeStream(is, null, o2);
////	        fis.close();
//	    } catch (IOException e) {
//	    }
//	    return b;
//	}

	public Bitmap[][] getmScreenButtonBg() {
		return this.mScreenButtonBg;
	}

	public BitmapDrawable getMainBg() {
		return this.mainBg;
	}

	public BitmapDrawable getMainBgMs() {
		return this.mainBgMs;
	}

	public Bitmap getScr2Text() {
		return this.scr2Text;
	}

	public Bitmap[][] getNumScreenNumbers() {
		return this.numScreenNumbers;
	}

	public Bitmap[] getColors() {
		return this.colors;
	}

	public Bitmap[][] getGeometry() {
		return geometry;
	}

	public Bitmap getCardScrText() {
		return cardScrText;
	}

	public Bitmap[][] getCards() {
		return cards;
	}

	public Bitmap getCardShirt() {
		return cardShirt;
	}

	public Bitmap getpScrTitle() {
		return pScrTitle;
	}

	public Bitmap[] getpScrBitmaps() {
		return pScrBitmaps;
	}

	public Bitmap getMainText() {
		return mainText;
	}
}
